package com.junlaninfo;

/**
 * Created by 辉 on 2020/7/30.
 * 证明volatile关键字的作用：子线程中用一个flag控制是否执行，这个flag由主线程改变状态
 */
public class Thread009 implements Runnable {
    private volatile static  boolean flag = true;

    @Override
    public void run() {
       while (flag){

       }
    }

    //在主线程中改变flag的状态
    public static void main(String[] args) {
        Thread t1 = new Thread(new Thread009(), "t1");
        t1.start();
        try {
            t1.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = false;
        System.out.println("主线程执行完毕");

    }
}
